package com.zhongge.controller;


import com.zhongge.entity.Teacher;
import com.zhongge.service.impl.TeacherServiceImpl;
import com.zhongge.vo.TeacherPswVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhongge
 * @since 2025-11-09
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherServiceImpl teacherService;

    //教师基本的增删改查：先写上 防止后面要使用到
    /**
     * 添加辅导员
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addTeacher(@RequestBody Teacher teacher) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = teacherService.save(teacher);
            result.put("success", success);
            result.put("message", success ? "添加成功" : "添加失败");

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 删除辅导员
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> deleteTeacher(@PathVariable Long id) {

        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = teacherService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 更新辅导员信息
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> updateTeacher(@RequestBody Teacher teacher) {

        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = teacherService.updateById(teacher);
            result.put("success", success);
            result.put("message", success ? "更新成功" : "更新失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据ID查询辅导员
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    public Map<String, Object> getTeacherById(@PathVariable Long id) {

        Map<String, Object> result = new HashMap<>();
        try {
            Teacher teacher = teacherService.getById(id);
            result.put("success", true);
            result.put("data", teacher);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败");
        }
        return result;
    }

    //辅导员登录功能
    @GetMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam("workId") String jobNumber,
                                     @RequestParam("password") String password,
                                     HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        Teacher teacher = teacherService.login(jobNumber, password);
        if (teacher != null) {
            //如果成功的话，就将信息存储于会话中
            session.setAttribute("LOGGED_IN_TEACHER",teacher);
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("message", "工号或密码错误");
        }
        return result;
    }

    //实现 修改密码功能
    //就是传递一个表单而已
    @ResponseBody
    @GetMapping("/changePassword")
    public  Map<String, Object> changePassword(@RequestBody TeacherPswVO teacherPswVO) {
        Map<String, Object> result = new HashMap<>();
        Long teacherId = teacherPswVO.getId();
        String oldPassword = teacherPswVO.getOldPassword();
        String newPassword = teacherPswVO.getNewPassword();

        boolean b = teacherService.changePassword(teacherId, oldPassword, newPassword);
        result.put("success", b);
        result.put("message", b ? "密码修改成功" : "原密码错误或用户不存在");
        return result;
    }

    //跳到登录页面
    @GetMapping("/indexPage")
    public String indexPage(){
        return "studestsys/login/login-select";
    }

    //调到辅导员首页
    @GetMapping("/teacherIndex")
    public String teacherIndex(){
        return "studestsys/teacher/counselor-index";
    }

    //从session获取教师ID（需要后端提供接口）
    @GetMapping("/session")
    @ResponseBody
    public Map<String, Object> session(HttpSession session){
        Map<String, Object> map = new HashMap<>();
        Object loggedInTeacher = session.getAttribute("LOGGED_IN_TEACHER");
        map.put("session",loggedInTeacher);
        return map;
    }

    //跳转到个人信息页面
    @GetMapping("/personalInfo")
    public String dispatchPersonalInfo(){
        return "studestsys/teacher/personal-info";
    }
}
